FROM jupyter/minimal-notebook

LABEL maintainer="pandare <kingsleyluoxin@hotmail.com>"

USER root

ENV UBUNTU_VERSION=bionic
ENV PANDA_BUILD=/home/panda/build

COPY cfe-3.3.src.tar.gz /home/panda-libs/cfe-3.3.src.tar.gz
COPY compiler-rt-3.3.src.tar.gz /home/panda-libs/compiler-rt-3.3.src.tar.gz
COPY libffi-3.0.13.tar.gz /home/panda-libs/libffi-3.0.13.tar.gz
COPY llvm-3.3.src.tar.gz /home/panda-libs/llvm-3.3.src.tar.gz
COPY panda.zip /home/panda-libs/panda.zip

RUN sed -i s/security.ubuntu.com/mirrors.163.com/g /etc/apt/sources.list && sed -i s/archive.canonical.com/mirrors.163.com/g /etc/apt/sources.list

RUN echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $UBUNTU_VERSION main restricted universe multiverse" > /etc/apt/sources.list && \
    echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $UBUNTU_VERSION main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $UBUNTU_VERSION-updates main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $UBUNTU_VERSION-updates main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $UBUNTU_VERSION-backports main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $UBUNTU_VERSION-backports main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $UBUNTU_VERSION-security main restricted universe multiverse" >> /etc/apt/sources.list && \
    echo "deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $UBUNTU_VERSION-security main restricted universe multiverse" >> /etc/apt/sources.list && \
    apt-get update

RUN apt-get -y build-dep qemu && apt-get install -y build-essential apt-transport-https ca-certificates curl gnupg2 software-properties-common git python-pip libc++-dev libelf-dev libdwarf-dev libelf-dev libdwarf-dev libwiretap-dev wireshark-dev python-pycparser protobuf-compiler protobuf-c-compiler python-protobuf   libprotoc-dev libprotobuf-dev libprotobuf-c-dev unzip zip gcc-5 g++-5 libcapstone-dev libdwarf-dev python-pycparser chrpath  apt-utils

RUN update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 50 && update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 50

WORKDIR /home/panda-libs

RUN tar zxvf libffi-3.0.13.tar.gz && cd libffi-3.0.13/ && ./configure --prefix=/usr --disable-static && make -j8 && make install

WORKDIR /home/panda-libs

RUN tar -xf llvm-3.3.src.tar.gz && cd llvm-3.3.src/ && tar -xf ../cfe-3.3.src.tar.gz -C tools && mv tools/cfe-3.3.src tools/clang

WORKDIR /home/panda-libs/llvm-3.3.src

ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

RUN sed -e 's:\$(PROJ_prefix)/docs/llvm:$(PROJ_prefix)/share/doc/llvm-3.3:' -i Makefile.config.in && CC=gcc CXX=g++ ./configure --prefix=/usr --sysconfdir=/etc --enable-libffi --enable-optimized --enable-shared --enable-targets=all --disable-assertions --disable-debug-runtime --disable-expensive-checks --enable-experimental-targets=R600 && make -j16 && make install 

WORKDIR /home/panda-libs

RUN git clone https://github.com/panda-re/panda.git && mv ./panda /home/panda && cd /home/panda && mkdir build && cd build && ../build.sh 

ENV PATH=/opt/conda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ENV PATH=$PATH:$PANDA_BUILD:$PANDA_BUILD/x86_64-softmmu:$PANDA_BUILD/i386-softmmu

WORKDIR $PANDA_BUILD/../panda/pypanda

RUN python3 setup.py install

RUN ln -s $PANDA_BUILD/x86_64-softmmu/panda-system-x86_64 /usr/bin/panda64 && ln -s $PANDA_BUILD/i386-softmmu/panda-system-i386 /usr/bin/panda32

# RUN useradd --create-home --no-log-init --shell /bin/bash pd
# RUN adduser pd sudo
# RUN echo 'pd:panda' | chpasswd

RUN cp -r /home/jovyan/* /home/pd

RUN conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ 
RUN conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/conda 
RUN conda config --set show_channel_urls yes

RUN pip install colorama protobuf -i https://pypi.tuna.tsinghua.edu.cn/simple

RUN rm -rf /home/panda-libs

RUN userdel -r jovyan

RUN mkdir -p /home/pre && \
    echo "pre:x:1000:1000:pre,,,:/home/pre:/bin/bash" >> /etc/passwd && \
    echo "pre:x:1000:" >> /etc/group && \
    echo "pre ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/pre && \
    chmod 0440 /etc/sudoers.d/pre && \
    chown 1000:1000 -R /home/pre

RUN apt-get install -y zsh

ENV HOME=/root

RUN echo "Y" | sh -c "$(curl -fsSL https://gitee.com/kingscat/oh-my-zsh/raw/master/tools/install.sh)"
RUN echo "/bin/zsh" | chsh
RUN sed -i s/ZSH_THEME=\"robbyrussell\"/ZSH_THEME=\"bureau\"/g ~/.zshrc

ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

USER pre

ENV HOME=/home/pre

RUN echo "Y" | sh -c "$(curl -fsSL https://gitee.com/kingscat/oh-my-zsh/raw/master/tools/install.sh)"
RUN echo "/bin/zsh" | sudo chsh
RUN sed -i s/ZSH_THEME=\"robbyrussell\"/ZSH_THEME=\"bureau\"/g ~/.zshrc

ENV NB_USER="pre"
ENV NB_UID="1000"
ENV NB_GID="1000"

WORKDIR /home/pre

CMD jupyter lab 1>jupyter.log 2>&1 &



